#include "MainMenuScene.h"
#include "LoginScene.h"
#include "ui/UIButton.h"
#include "ui/CocosGUI.h"
#include "ui/UIButton.h"
#include "Scene/PlantChooseScene.h"
#include "Scene/HelpScene.h"
#include "Scene/SettingScene.h"
#include "Plant/SunFlower.h"
#include "Plant/PlantBase.h"
#include "Plant/WallNut.h"
#include "Plant/Peashooter.h"
#include "Plant/Sun.h"
#include "Plant/Squash.h"
#include "Plant/PlantBase.h"
#include "Other/Data.h"
using namespace std;

USING_NS_CC;

Scene* MainMenuScene::createScene() {
    return MainMenuScene::create();
}           

// Print useful error message instead of segfaulting when files are not there.
static void problemLoading(const char* filename) {
    printf("Error while loading: %s\n", filename);
    printf("Depending on how you compiled you might have to add 'Resources/' in front of filenames in MainMenuScene.cpp\n");
}

// on "init" you need to initialize your instance
bool MainMenuScene::init() {
    if (!Scene::init()) {
        return false;
    }

    auto visibleSize = Director::getInstance()->getVisibleSize();
    Vec2 origin = Director::getInstance()->getVisibleOrigin();
    auto mainMenuBGSprite = Sprite::create("Games/bg0.jpg");
    mainMenuBGSprite->setPosition(Vec2(visibleSize.width / 2 + origin.x, visibleSize.height / 2 + origin.y));
    mainMenuBGSprite->setScale(2.1); // 调整大小
    this->addChild(mainMenuBGSprite, 0);

    //背景图片设置
    
    //创建鼠标监听器
    //auto mouser_listener = EventListenerMouse::create();
    //mouser_listener->onMouseDown = [&](Event* event)
    //{
    //    auto mouse = (EventMouse*)event;
    //    int x = mouse->getCursorX();
    //    int y = mouse->getCursorY();
    //    if (x >= 1290)
    //    {

    //    }
    //};

    
    //按钮 ： 除了退出按钮之外  全部使用Ui::button 实现
    //游戏按钮 切换到 PlantChooseScene
    auto switch_to_PlantChooseScene = ui::Button::create("Games/Adventure_0.png",
                                                                         "Games/Adventure_1.png",
                                                                          "Games/Adventure_0.png");
    this->addChild(switch_to_PlantChooseScene,1);
    switch_to_PlantChooseScene->setScale(3.9);
    switch_to_PlantChooseScene->setPosition(Vec2(630, 360));
    switch_to_PlantChooseScene->addTouchEventListener([&](Ref* sender, ui::Widget::TouchEventType type)
        {
            switch (type)
            {
            case ui::Widget::TouchEventType::BEGAN:
                break;
            case ui::Widget::TouchEventType::ENDED:
                // 加载界面/主界面BGM停止
                AudioEngine::stop(LoginScene::login_BGM);
                auto PlantChoseScene = PlantChooseScene::create();
                //Sleep(1); 
                Director::getInstance()->pushScene(PlantChoseScene); 
                break;
            }
        });
    //设置按钮  切换到 SettingScene
    
    //没有合适的图片资源
    //加载以下图片,并设置为透明
    auto switch_to_SettingScene = ui::Button::create("Games/GameLoose.png",
                                                                          "Games/GameLoose.png",
                                                                       "Games/GameLoose.png");
    this->addChild(switch_to_SettingScene,2);
    switch_to_SettingScene->setScale(0.2);
    switch_to_SettingScene->setPosition(Vec2(690,80));
    switch_to_SettingScene->addTouchEventListener([&](Ref* sender, ui::Widget::TouchEventType type)
        {
            switch (type)
            {
            case ui::Widget::TouchEventType::BEGAN:
                break;
            case ui::Widget::TouchEventType::ENDED:
                auto SettingScene = SettingScene::create();
                //Sleep(1); 
                Director::getInstance()->pushScene(SettingScene);
                break;
            }
        });
    switch_to_SettingScene->setOpacity(0);

    //帮助按钮 切换到HelpScene

    auto switch_to_HelpScence = ui::Button::create("Games/GameLoose.png" ,
                                                                   "Games/GameLoose.png",
                                                                    "Games/GameLoose.png");
    this->addChild(switch_to_HelpScence, 3);
    switch_to_HelpScence->setScale(0.2);
    switch_to_HelpScence->setPosition(Vec2(770,70));
    switch_to_HelpScence->addTouchEventListener([&](Ref* sender, ui::Widget::TouchEventType type)
        {
            switch (type)
            {
            case ui::Widget::TouchEventType::BEGAN:
                break;
            case ui::Widget::TouchEventType::ENDED:
              auto HelpScence = HelpScene::create();
              Director::getInstance()->pushScene(HelpScence);
                break;
            }
        });

    switch_to_HelpScence->setOpacity(0);
    //退出按钮 调用  menuCloseCallback(Ref* pSender) 
    
    auto close = ui::Button::create("Games/GameLoose.png",
                                                    "Games/GameLoose.png",
                                                      "Games/GameLoose.png");
    this->addChild(close,5);
    close->setScale(0.2);
    close->setPosition(Vec2(850,80));
    close->addTouchEventListener([&](Ref* sender, ui::Widget::TouchEventType type)
        {
            switch (type)
            {
            case ui::Widget::TouchEventType::BEGAN:
                break;
            case ui::Widget::TouchEventType::ENDED:
                MainMenuScene::menuCloseCallback(sender);  //修改下
                break;
            }
        });
    close->setOpacity(0);
    return true;
}


void MainMenuScene::menuCloseCallback(Ref* pSender) {
    Director::getInstance()->end();
}
